import requests
import re
import os
#2021-05-05 13:21 by 炸天帮成员
#通过浏览器F12可知，B站视频多为m4s格式文件。
url="https://upos-sz-mirrorcoso1.bilivideo.com/upgcxcode/74/08/248790874/248790874_nb2-1-30280.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1620202558&gen=playurlv2&os=coso1bv&oi=2062930117&trid=40948b28c6a64a1bbbe7a6340cf96f27p&platform=pc&upsig=4e7d50214d6f5e961e3176c249ac5cea&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&mid=0&orderid=0,3&agrr=1&logo=80000000"
#注意Range为response-headers中的conent-range的最大值。
headers={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3861.400 QQBrowser/10.7.4313.400",
             "Range":"bytes=0-29502588",
             "referer":"https://www.bilibili.com/bangumi/play/ep391229?bsource=baidu_aladdin",
             "connection":"keep-alive"}
r=requests.get(url,headers=headers)
voiceFile_m4s="_7_bilibili暴走《大事件》第7季第一期(下)_voice.m4s"
f=open(voiceFile_m4s,"wb")
f.write(r.content)
f.close()
print("音频部分已经下载完成")

f=open(voiceFile_m4s,"rb")
voiceFile=f.read()
f.close()
voiceFile_mp4="_7_bilibili暴走《大事件》第7季第一期(下)_voice.mp4"
f=open(voiceFile_mp4,"wb")
f.write(voiceFile)
f.close()


url="https://upos-sz-mirrorcoso1.bilivideo.com/upgcxcode/74/08/248790874/248790874_nb2-1-30032.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1620202558&gen=playurlv2&os=coso1bv&oi=2062930117&trid=40948b28c6a64a1bbbe7a6340cf96f27p&platform=pc&upsig=199114e0fb6b899825b8b3f373ec9881&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&mid=0&orderid=0,3&agrr=1&logo=80000000"
headers={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3861.400 QQBrowser/10.7.4313.400",
             "Range":"bytes=0-67514155",
             "referer":"https://www.bilibili.com/bangumi/play/ep391229?bsource=baidu_aladdin",
             "connection":"keep-alive"}
r=requests.get(url,headers=headers)
videoFile_m4s="_7_bilibili暴走《大事件》第7季第一期(下)_video.m4s"
f=open(videoFile_m4s,"wb")
f.write(r.content)
f.close()
print("视频部分已经下载完成")

f=open(videoFile_m4s,"rb")
videoFile=f.read()
f.close()
videoFile_mp4="_7_bilibili暴走《大事件》第7季第一期(下)_video.mp4"
f=open(videoFile_mp4,"wb")
f.write(videoFile)
f.close()

print("后续操作：")
print("1.自行下载:'格式工厂'软件")
print("2.打开'格式工厂'，选择'视频合并&混流',选择'.All files'")
print("3.将_7_bilibili暴走《大事件》第7季第一期(下)_video.m4s放入'视频'窗口;")
print("4.将_7_bilibili暴走《大事件》第7季第一期(下)_voice.m4s放入'音频'窗口;")
print("5.选择'确定'，在'格式工厂'软件最上方选择'开始',找到转换完毕的.mp4文件，即可播放。")
